# -------------------------------------------------------------------
# Purpose: Creates Table B49
# Author:  Max Posch, 25/07/2025
# Usage:   Source this script to generate the table.
# -------------------------------------------------------------------
# Check that required paths exist
stopifnot(dir.exists(pdataconfanalysis))
stopifnot(dir.exists(poutputappendix))


# Load data
load(file.path(pdataconfanalysis, "patentLevel19001940.RData"))


# Regressions
o <- list()
o <- append(o, list(feols(break_p80_rrfsim05 ~ entropy_patent_namelast_mp_relgendist_max_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
o <- append(o, list(feols(break_p80_rrfsim05 ~ entropy_patent_namelast_mp_relgendist_max_adjp_ws  | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
o <- append(o, list(feols(break_p80_rrfsim05 ~ entropy_patent_namelast_mp_relgendist_max_adjp_ws | no_of_inventors + gisjoin_1900^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
o <- append(o, list(feols(n_uspto_techn ~ entropy_patent_namelast_mp_relgendist_max_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
o <- append(o, list(feols(n_uspto_techn ~ entropy_patent_namelast_mp_relgendist_max_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
o <- append(o, list(feols(n_uspto_techn ~ entropy_patent_namelast_mp_relgendist_max_adjp_ws | no_of_inventors + gisjoin_1900^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))

r <- list()
r <- append(r, list(feols(break_p80_rrfsim05 ~ iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws | no_of_inventors + gisjoin_1900 + statefip^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
r <- append(r, list(feols(break_p80_rrfsim05 ~ iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws  | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
r <- append(r, list(feols(break_p80_rrfsim05 ~ iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
r <- append(r, list(feols(n_uspto_techn ~ iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws | no_of_inventors + gisjoin_1900 + statefip^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
r <- append(r, list(feols(n_uspto_techn ~ iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
r <- append(r, list(feols(n_uspto_techn ~ iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))

i <- list()
i <- append(i, list(feols(break_p80_rrfsim05 ~ 1 | no_of_inventors + gisjoin_1900 + statefip^year | entropy_patent_namelast_mp_relgendist_max_adjp_ws ~ iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws, patentLevel19001940, weight = patentLevel19001940$wgt)))
i <- append(i, list(feols(break_p80_rrfsim05 ~ 1 | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year | entropy_patent_namelast_mp_relgendist_max_adjp_ws ~ iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws, patentLevel19001940, weight = patentLevel19001940$wgt)))
i <- append(i, list(feols(break_p80_rrfsim05 ~ 1 | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year | entropy_patent_namelast_mp_relgendist_max_adjp_ws ~ iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws, patentLevel19001940, weight = patentLevel19001940$wgt)))
i <- append(i, list(feols(n_uspto_techn ~ 1 | no_of_inventors + gisjoin_1900 + statefip^year | entropy_patent_namelast_mp_relgendist_max_adjp_ws ~ iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws, patentLevel19001940, weight = patentLevel19001940$wgt)))
i <- append(i, list(feols(n_uspto_techn ~ 1 | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year | entropy_patent_namelast_mp_relgendist_max_adjp_ws ~ iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws, patentLevel19001940, weight = patentLevel19001940$wgt)))
i <- append(i, list(feols(n_uspto_techn ~ 1 | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year | entropy_patent_namelast_mp_relgendist_max_adjp_ws ~ iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws, patentLevel19001940, weight = patentLevel19001940$wgt)))


# F-statistics
dstata <- patentLevel19001940[, .(
  x1 = entropy_patent_namelast_mp_relgendist_max_adjp_ws, 
  z1 = iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws,
  y1 = break_p80_rrfsim05, 
  y2 = n_uspto_techn, 
  wgt, gisjoin_1900_f, statefip_f, year_f, no_of_inventors_f, tech_category_f
)]
commands <- list(
  'ivreghdfe y1 (x1= z1) [pw=wgt], absorb(no_of_inventors_f gisjoin_1900_f year_f#statefip_f) cluster(gisjoin_1900_f) first ffirst savefirst
   gen swf1 = .
   replace swf1 = round(e(first)["SWF",1])
   keep swf*
   keep if _n == 1',
  'ivreghdfe y1 (x1 = z1) [pw=wgt], absorb(no_of_inventors_f gisjoin_1900_f year_f#statefip_f tech_category_f#year_f) cluster(gisjoin_1900_f) first ffirst savefirst
   gen swf1 = .
   replace swf1 = round(e(first)["SWF",1])
   keep swf*
   keep if _n == 1',
  'ivreghdfe y1 (x1= z1) [pw=wgt], absorb(no_of_inventors_f gisjoin_1900_f year_f#statefip_f year_f#tech_category_f) cluster(gisjoin_1900_f) first ffirst savefirst
   gen swf1 = .
   replace swf1 = round(e(first)["SWF",1])
   keep swf*
   keep if _n == 1'
)
swf_results <- as.character(unlist(lapply(commands, get_fstat_from_stata, data.in = dstata)))
swfstat <- rep(swf_results, times = 2)


# Create table
y1_mean <- round(weighted.mean(patentLevel19001940$break_p80_rrfsim05, patentLevel19001940$wgt), 2)
y1_sd <- round(sqrt(weighted.mean((patentLevel19001940$break_p80_rrfsim05 - y1_mean)^2, patentLevel19001940$wgt)), 2)
y2_mean <- round(weighted.mean(patentLevel19001940$n_uspto_techn, patentLevel19001940$wgt), 2)
y2_sd <- round(sqrt(weighted.mean((patentLevel19001940$n_uspto_techn - y2_mean)^2, patentLevel19001940$wgt)), 2)
y1 <- paste0("\\makecell{Breakthrough patent \\\\ indicator \\\\ (mean = ", y1_mean, ", sd = ", y1_sd, ")}")
y2 <- paste0("\\makecell{Technologies \\\\ per patent \\\\ (mean = ", y2_mean, ", sd = ", y2_sd, ")}")

setFixest_dict(
  c(
    entropy_patent_namelast_mp_relgendist_max_adjp_ws = "Genetic distance weighted surname diversity of patent",
    iv_lo_entropy_namelast_mp_relgendist_max_adjp_fe_immig_ws = "Predicted genetic distance weighted surname diversity",
    break_p80_rrfsim05 = y1, n_uspto_techn = y2,
    year = "Period", statefip = "State", namelast_mp = "Surname", gisjoin_1900 = "County", main_patentclass = "Patent technology class", tech_category = "Patent technology field", no_of_inventors = "Team size"
  )
)

tablename <- file.path(poutputappendix, "tableB49.tex")
etable(o,
  cluster = ~gisjoin_1900,
  fitstat = ~n,
  digits = "r3", digits.stats = "r3",
  file = tablename, replace = TRUE,
  style.tex = style.tex("aer"), tex = TRUE
)
add_table_row(tablename, "\\midrule", "\\multicolumn{2}{l}{\\textit{Panel A: Least-squares estimates}} &  \\multicolumn{5}{c}{}\\\\ \\cmidrule(lr){1-7}")
add_table_row(tablename, "mean =", "\\cmidrule(lr){2-4}  \\cmidrule(lr){5-7}")
move_table_row(tablename, "Observations", "bottomrule")
add_table_row(tablename, "    \\\\", c("\\multicolumn{2}{l}{\\textit{Panel B: Reduced-form estimates}} &  \\multicolumn{5}{c}{}\\\\", "\\\\", "\\multicolumn{2}{l}{\\textit{Panel C: Instrumental-variable estimates}} &  \\multicolumn{5}{c}{}\\\\", "\\\\", "\\multicolumn{2}{l}{\\textit{Panel D: First-stage estimates}} &  \\multicolumn{5}{c}{}\\\\"))

temptable <- file.path(poutputappendix, "temp.tex")
etable(r,
  cluster = ~gisjoin_1900,
  fitstat = ~n,
  digits = "r3", digits.stats = "r3",
  file = temptable, replace = TRUE,
  style.tex = style.tex("aer"), tex = TRUE
)
estimates_rows <- get_estimates_rows(temptable)
estimates_rows <- collapse_stage1(estimates_rows, c(2, 3, NA, 5, 6, NA))
add_table_row(tablename, "Panel B", c("\\cmidrule(lr){1-7}", estimates_rows))

temptable <- file.path(poutputappendix, "temp.tex")
etable(i,
  cluster = ~gisjoin_1900,
  fitstat = ~n,
  digits = "r3", digits.stats = "r3",
  extralines = list("Sanderson-Windmeijer \\textit{F}-stat" = swfstat),
  file = temptable, replace = TRUE,
  style.tex = style.tex("aer"), tex = TRUE
)
estimates_rows <- get_estimates_rows(temptable)
estimates_rows <- collapse_stage1(estimates_rows, c(2, 3, NA, 5, 6, NA))
add_table_row(tablename, "Panel C", c("\\cmidrule(lr){1-7}", estimates_rows))
add_table_row(tablename, "Sanderson", "\\\\", "before")

temptable <- file.path(poutputappendix, "temp.tex")
etable(i,
  stage = 1,
  cluster = ~gisjoin_1900,
  fitstat = ~n, digits = "r3", digits.stats = "r3",
  file = temptable, replace = TRUE,
  style.tex = style.tex("aer"), tex = TRUE
)
estimates_rows <- get_estimates_rows(temptable)
estimates_rows <- collapse_stage1(estimates_rows, c(2, 3, NA, NA, NA, NA))
add_table_row(tablename, "Panel D", estimates_rows)
add_table_row(tablename, "Panel D", c("\\cmidrule(lr){1-7}", "& \\multicolumn{2}{c}{\\makecell{Genetic distance weighted \\\\ surname diversity of patent}} & \\\\", "\\cmidrule(lr){2-3}"))
file.remove(temptable)

cat("Table B49 saved to:", tablename, "\n")